package com.woniuxy.service.impl;

import com.woniuxy.controller.info.MenuInfo;
import com.woniuxy.model.Menus;
import com.woniuxy.dao.MenusDao;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.service.MenusService;
import com.woniuxy.utils.NewCopyBean;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hujianying
 * @since 2022-03-23
 */
@Service
public class MenusServiceImpl extends ServiceImpl<MenusDao, Menus> implements MenusService {


    /**
     *
     * @return data为allMenus菜单集合
     */
    @Override
    public List<MenuInfo> getAllMenus(Integer accountId) {
        List<Menus> menues = baseMapper.findMenueByAcountId(accountId);
        List<MenuInfo> menuInfos = NewCopyBean.copyList(menues, MenuInfo.class);
        List<MenuInfo> arlist = new ArrayList<>();
        for (MenuInfo menusInfo : menuInfos) {
            if (menusInfo.getMenusParentid()==0) {
                //如果是一个根目录，放入集合
                arlist.add(menusInfo);
                for (MenuInfo menusInfo2 : menuInfos) {
                    if (menusInfo2.getMenusParentid()==menusInfo.getMenusId()) {
                        if(menusInfo.getChildren()==null) menusInfo.setChildren(new ArrayList<>());
                        menusInfo.getChildren().add(menusInfo2);
                    }
                }
            }
        }
        return arlist;
    }

    @Override
    public List<MenuInfo> findMenueByruleId(Integer id) {

        List<Menus> menues = list();
        List<MenuInfo> menuInfos = NewCopyBean.copyList(menues, MenuInfo.class);
        ArrayList<MenuInfo> arlist = new ArrayList<>();
        for (MenuInfo menusInfo : menuInfos) {
            if (menusInfo.getMenusParentid()==0) {
                //如果是一个根目录，放入集合
                arlist.add(menusInfo);
                for (MenuInfo menusInfo2 : menuInfos) {
                    if (menusInfo2.getMenusParentid()==menusInfo.getMenusId()) {
                        if(menusInfo.getChildren()==null) menusInfo.setChildren(new ArrayList<>());
                        menusInfo.getChildren().add(menusInfo2);
                    }
                }
            }
        }
        return arlist;
    }
}
